<?php
/**
 * Created by PhpStorm.
 * User: xiaomeng
 * Date: 17-7-12
 * Time: 下午3:14
 */
class A
{
    public $sypool;
    public $freePool;
    public $max_con = 20;
    public function __construct()
    {
        $this->freePool = new SplQueue();
        $this->sypool = new SplQueue();
    }

    public function createOne()
    {
        if(count($this->freePool) == 0 && count($this->sypool) >= $this->max_con)
        {
            throw new Exception('连接池使用完毕!');
        }
        $redis = new Redis();
        $redis->connect('127.0.0.1',6379);
        $this->freePool->unshift($redis);
    }
    public function getOne()
    {
        if(count($this->freePool) >0)
        {
            $redis = $this->freePool->pop();
            $this->sypool->unshift($redis);
        }
        else
        {
            $this->createOne();
            $this->createOne();
            $redis = $this->freePool->pop();
            $this->sypool->unshift($redis);
        }
        var_dump($this->freePool);
            return $redis;
    }
}

$a = new A();
//$a->getOne();
var_dump($a->getOne());